package com.cmes.mes.basic.web.controller;

import com.cmes.core.Pagination;
import com.cmes.core.utils.CollectionUtils;
import com.cmes.jdbc.customquery.CustomQueryBO;
import com.cmes.mes.basic.api.bo.WarehouseTypeBO;
import com.cmes.mes.basic.core.entity.WarehouseEntity;
import com.cmes.mes.basic.core.service.WarehouseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/mes/warehouse")
public class WarehouseController {

    private final WarehouseService warehouseCacheService;

    @PostMapping("search")
    public Pagination<WarehouseEntity> search(@RequestBody @Valid CustomQueryBO bo) {
        return warehouseCacheService.page(bo);
    }

    @PostMapping("selector")
    public List<WarehouseEntity> selector(@RequestBody @Valid WarehouseTypeBO bo) {
        if (CollectionUtils.isEmpty(bo.getType())) {
            return warehouseCacheService.active();
        } else {
            return warehouseCacheService.active().stream()
                    .filter(v -> bo.getType().contains(v.getType()))
                    .collect(Collectors.toList());
        }
    }

}
